using System;
using System.Collections.Generic;
using System.Text;
using System.CodeDom;

namespace Destra.Tools.DACGenerator.CodeGenerators
{
    public class CodeDomHelper
    {
        private CodeDomHelper()
        {

        }

        public static CodeMemberField AddFieldToClass(
            CodeTypeDeclaration codeTypeDeclaration,
            MemberAttributes attributes,
            string name,
            Type type,
            bool nullable)
        {
            CodeMemberField codeMemberField = new CodeMemberField();
            codeMemberField.Attributes = attributes;
            codeMemberField.Name = name.Replace("@", "");
            if ((type.IsValueType == true) && (nullable == true))
            {
                codeMemberField.Type = new CodeTypeReference(
                    "System.Nullable",
                    new CodeTypeReference[]
						{
						new CodeTypeReference(type)
						}
                    );
            }
            else
            {
                codeMemberField.Type = new CodeTypeReference(type);
            }

            if (codeTypeDeclaration != null)
            {
                codeTypeDeclaration.Members.Add(codeMemberField);
            }

            return codeMemberField;
        }

        public static CodeMemberProperty AddSimplePropertyToClass(
            CodeTypeDeclaration codeTypeDeclaration,
            MemberAttributes attributes,
            string name,
            Type type,
            string fieldName,
            bool nullable)
        {
            CodeMemberProperty codeMemberProperty = new CodeMemberProperty();
            codeMemberProperty.Attributes = attributes;
            codeMemberProperty.Name = name.Replace("@", "");
            if ((type.IsValueType == true) && (nullable == true))
            {
                codeMemberProperty.Type = new CodeTypeReference(
                    "System.Nullable",
                    new CodeTypeReference[]
					{
						new CodeTypeReference(type)
					}
                    );
            }
            else
            {
                codeMemberProperty.Type = new CodeTypeReference(type);
            }

            codeMemberProperty.GetStatements.Add(
                new CodeMethodReturnStatement(
                    new CodeFieldReferenceExpression(
                        new CodeThisReferenceExpression(),
                        fieldName
                        )
                    )
                );

            codeMemberProperty.SetStatements.Add(
                new CodeAssignStatement(
                    new CodeFieldReferenceExpression(
                        new CodeThisReferenceExpression(),
                        fieldName
                        ),
                    new CodeVariableReferenceExpression(
                        "value"
                        )
                    )
                );

            if (codeTypeDeclaration != null)
            {
                codeTypeDeclaration.Members.Add(codeMemberProperty);
            }

            return codeMemberProperty;
        }

    }
}
